{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2017-03-01 18:00:00')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳 .Timestamp（）\n",
    "pd.Timestamp('2017-03-01 18:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-04-24 00:14:15')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(1587687255,unit=\"s\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([datetime.time(8, 30), datetime.time(8, 50), datetime.time(9, 10),\n",
       "       datetime.time(9, 30), datetime.time(9, 50), datetime.time(10, 10),\n",
       "       datetime.time(10, 30), datetime.time(10, 50),\n",
       "       datetime.time(11, 10), datetime.time(11, 30),\n",
       "       datetime.time(11, 50), datetime.time(12, 10),\n",
       "       datetime.time(12, 30), datetime.time(12, 50),\n",
       "       datetime.time(13, 10), datetime.time(13, 30),\n",
       "       datetime.time(13, 50), datetime.time(14, 10),\n",
       "       datetime.time(14, 30), datetime.time(14, 50),\n",
       "       datetime.time(15, 10), datetime.time(15, 30),\n",
       "       datetime.time(15, 50), datetime.time(16, 10),\n",
       "       datetime.time(16, 30), datetime.time(16, 50)], dtype=object)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间序列\n",
    "pd.date_range(start=pd.Timestamp(\"2021-12-22 8:30\"), end=\"2021-12-22 17:00\", freq=\"20min\").time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-12-22 08:30:00', '2021-12-22 08:50:00',\n",
       "               '2021-12-22 09:10:00', '2021-12-22 09:30:00',\n",
       "               '2021-12-22 09:50:00', '2021-12-22 10:10:00',\n",
       "               '2021-12-22 10:30:00', '2021-12-22 10:50:00',\n",
       "               '2021-12-22 11:10:00', '2021-12-22 11:30:00',\n",
       "               '2021-12-22 11:50:00', '2021-12-22 12:10:00',\n",
       "               '2021-12-22 12:30:00', '2021-12-22 12:50:00',\n",
       "               '2021-12-22 13:10:00', '2021-12-22 13:30:00',\n",
       "               '2021-12-22 13:50:00', '2021-12-22 14:10:00',\n",
       "               '2021-12-22 14:30:00', '2021-12-22 14:50:00',\n",
       "               '2021-12-22 15:10:00', '2021-12-22 15:30:00',\n",
       "               '2021-12-22 15:50:00', '2021-12-22 16:10:00',\n",
       "               '2021-12-22 16:30:00', '2021-12-22 16:50:00'],\n",
       "              dtype='datetime64[ns]', freq='20T')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#转化为时间戳\n",
    "pd.to_datetime(pd.date_range(start=pd.Timestamp(\"2021-12-22 8:30\"), end=\"2021-12-22 17:00\", freq=\"20min\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "别名|描述|别名|描述\n",
    ":-|:-|:-|:-\n",
    "B|工作日频率|BQS|工作季度开始频率\n",
    "D|日历日频率|A|年终频率\n",
    "W|每周频率|BA|工作年度结束频率\n",
    "M|月末频率|BAS|工作年度开始频率\n",
    "SM|半月结束频率|BH|营业时间频率\n",
    "BM|工作月结束频率|H|小时频率\n",
    "MS|月开始频率|T,min|每分钟频率\n",
    "SMS|半月开始频率|S|每秒钟频率\n",
    "BMS|工作月开始频率|L,ms|毫秒\n",
    "Q|季末频率|U,us|微妙\n",
    "BQ|工作季度结束频率\tN|纳秒\n",
    "QS|季度开始频率\t \t "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2014-01-01 00:10', '10T')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间周期计算\n",
    "pd.Period('2014', freq='10min') + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06',\n",
       "             '2016-07', '2016-08', '2016-09', '2016-10', '2016-11', '2016-12',\n",
       "             '2017-01', '2017-02', '2017-03', '2017-04', '2017-05', '2017-06',\n",
       "             '2017-07', '2017-08', '2017-09', '2017-10', '2017-11', '2017-12',\n",
       "             '2018-01'],\n",
       "            dtype='period[M]', freq='M')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建时间周期\n",
    "pd.period_range('2016','2018', freq='M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2008-01-01    0.112106\n",
      "2008-01-02    0.343857\n",
      "2008-01-03    0.306291\n",
      "2008-01-04    0.592763\n",
      "2008-01-05   -0.437986\n",
      "dtype: float64\n",
      "2020-01    0.065852\n",
      "2020-02    0.188920\n",
      "2020-03    0.814543\n",
      "2020-04    0.245630\n",
      "2020-05    0.887487\n",
      "2020-06    0.261271\n",
      "2020-07    0.309383\n",
      "2020-08    0.795630\n",
      "2020-09    0.630025\n",
      "2020-10    0.046527\n",
      "2020-11    0.113492\n",
      "2020-12    0.486543\n",
      "2021-01    0.951665\n",
      "Freq: M, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 时间序列 datetimeIndex\n",
    "date = pd.DatetimeIndex(['1/1/2008', '1/2/2008', '1/3/2008', '1/4/2008', '1/5/2008'])\n",
    "dt = pd.Series(np.random.randn(5),index = date)\n",
    "print(dt)\n",
    "\n",
    "date = pd.period_range(\"2020\", \"2021\", freq=\"M\")\n",
    "dt = pd.Series(np.random.rand(13),index=date)\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 days 06:00:00\n"
     ]
    }
   ],
   "source": [
    "# 时间差\n",
    "print (pd.Timedelta(days=2,hours=6))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
